package com.kd.juhaowan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.PhoneCodeApi;
import com.kd.juhaowan.http.api.RegistApi;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.model.LoginUser;
import com.kd.juhaowan.utils.MsgCountDown;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.hjq.toast.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RegistActivity extends BaseActivity{


    @BindView(R.id.regist_phonetab_layout)
    LinearLayout phoneTabLayout;
    @BindView(R.id.regist_accounttab_layout)
    LinearLayout accountTabLayout;
    @BindView(R.id.regist_phonetab_line)
    View phoneTabLine;
    @BindView(R.id.regist_accounttab_line)
    View accountTabLine;

    @BindView(R.id.regist_phone_layout)
    LinearLayout phoneLayout;
    @BindView(R.id.regist_accout_layout)
    LinearLayout accountLayout;
    @BindView(R.id.regist_phone_phone)
    EditText phoneEdit;
    @BindView(R.id.regist_phone_code_edit)
    EditText phoneCodeEdit;
    @BindView(R.id.regist_phone_pwd)
    EditText phonePwdEdit;
    @BindView(R.id.regist_account_account)
    EditText accountEdit;
    @BindView(R.id.regist_account_pwd)
    EditText accountPwdEdit;
    @BindView(R.id.regist_phone_code_get)
    TextView phoneCodeGet;
    @BindView(R.id.regist_checkbox)
    CheckBox mCheckBox;



    private boolean isPhoneRegist =true;


    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_regist;
    }

    @OnClick(R.id.regist_accounttab_layout)
    public void onAccountTabClick(){
        if(isPhoneRegist){
            isPhoneRegist =false;
            phoneTabLayout.setBackgroundColor(getColor(R.color.cee));
            phoneTabLine.setVisibility(View.INVISIBLE);

            accountTabLayout.setBackgroundColor(getColor(android.R.color.transparent));
            accountTabLine.setVisibility(View.VISIBLE);
            phoneLayout.setVisibility(View.GONE);
            accountLayout.setVisibility(View.VISIBLE);
        }
    }

    @OnClick(R.id.regist_phonetab_layout)
    public void onPhoneTabClick(){
        if(!isPhoneRegist){
            isPhoneRegist = true;

            accountTabLayout.setBackgroundColor(getColor(R.color.cee));
            accountTabLine.setVisibility(View.INVISIBLE);

            phoneTabLayout.setBackgroundColor(getColor(android.R.color.transparent));
            phoneTabLine.setVisibility(View.VISIBLE);
            accountLayout.setVisibility(View.GONE);
            phoneLayout.setVisibility(View.VISIBLE);

        }
    }

    private boolean isAuth= false;
    private boolean isPay= false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        List<LoginUser> infos = manager.readAllUserAccountInfo();
        isAuth= getIntent().getBooleanExtra("isAuth",false);
        isPay = getIntent().getBooleanExtra("isPay",false);
//        int loType= manager.readLastLoginType();
////        List<LoginUser> infosNew = new ArrayList<>();
//        if(infos!=null&&infos.size()>0){
//            startActivity(new Intent(RegistActivity.this,LoginAccountActivity.class));
//            finish();
//        }
    }

    @OnClick(R.id.regist_tologin)
    public void goLogin(){
        setResult(RESULT_OK);
        finish();
//        Intent intent = new Intent(RegistActivity.this,LoginAccountActivity.class);
//        intent.putExtra("fromRegist",true);
//        intent.putExtra("isAuth",isAuth);
//        intent.putExtra("isPay",isPay);
//        startActivity(intent);
//            finish();
    }

    @Override
    protected void reInitDate() {

    }

    @OnClick(R.id.regist_phone_code_get)
    public void getPhoneCode(View view){
        String account = phoneEdit.getText().toString().trim();
        if(TextUtils.isEmpty(account)){
            ToastUtils.show("请输入手机号码");
            return;
        }
        EasyHttp.post(this).api(new PhoneCodeApi(account,"sms_reg")).request(new HttpCallbackProxy<HttpData<LoginUser>>(this){
            @Override
            public void onHttpSuccess(HttpData<LoginUser> result) {
                super.onHttpSuccess(result);
                ToastUtils.show("获取验证码成功");
                MsgCountDown.setCurrentTime();
                updateSmsCount();
            }
        });
    }


    private void updateSmsCount() {
        if (isFinishing()||isDestroyed()) {
            return;
        }

        int value = (int) ((System.currentTimeMillis() - MsgCountDown.getLastTime()) / 1000);
        if (value > 59) {
            phoneCodeGet.setEnabled(true);
            phoneCodeGet.setText("重新发送");
//            loginCode.setTextColor(Color.parseColor("#ffffff"));
        } else {
//            reGetCode.setTextColor(Color.parseColor("#a39dce"));
//			reGetCode.setTextColor(ViewUtil.getColorRs(activity,"fjd_white"));
            phoneCodeGet.setEnabled(false);
            phoneCodeGet.setText((60 - value) + "s");
            phoneCodeGet.postDelayed(new Runnable() {

                @Override
                public void run() {
                    updateSmsCount();
                }
            }, 1000);
        }
    }

    private void gotoMainActivity(){
        if(isAuth){
            startActivity(new Intent(this,AuthActivity.class));
        }else if(isPay){
            startActivity(new Intent(this,PlatFormCoinActivity.class));
        }
        else {
            startActivity(new Intent(this,MainActivity.class));
        }
        finish();
    }

    @OnClick(R.id.regist_confirm)
    public void onLoginClick(View view){
        if(!mCheckBox.isChecked()){
            ToastUtils.show("请阅读并同意协议");
            return;
        }
        if(isPhoneRegist){
            String phone = phoneEdit.getText().toString().trim();
            String code = phoneCodeEdit.getText().toString().trim();
            String pwd = phonePwdEdit.getText().toString().trim();
            if(TextUtils.isEmpty(phone)){
                ToastUtils.show("请输入手机号");
            }else if(TextUtils.isEmpty(code)){
                ToastUtils.show("请输入验证码");
            }else if(TextUtils.isEmpty(pwd)){
                ToastUtils.show("请输入密码");
            }else{
                EasyHttp.post(this).api(new RegistApi(phone,pwd,"mobile","sms_reg",code)).request(new HttpCallbackProxy<HttpData<LoginUser>>(this){
                    @Override
                    public void onHttpSuccess(HttpData<LoginUser> result, boolean cache) {
                        super.onHttpSuccess(result, cache);
                        ToastUtils.show("注册成功");
                        LoginUser user = result.getData();
                        user.setAccount(phone);
                        user.setPassword(pwd);
                        user.setPhoneAccount(true);
                        user.setUsername(phone);
                        manager.saveUserAccount(result.getData());
                        manager.writeAutoLogin(true);
                        manager.writeLastLoginType(1);
                        manager.saveCurrentLoginUserTickit(user.getAccess_token());
                        gotoMainActivity();
                    }
                });
            }
        }else{
            String account = accountEdit.getText().toString().trim();
            String pwd = accountPwdEdit.getText().toString().trim();
            if(TextUtils.isEmpty(account)){
                ToastUtils.show("请输入账号");
            }else if(TextUtils.isEmpty(pwd)){
                ToastUtils.show("请输入密码");
            }else {
                EasyHttp.post(this).api(new RegistApi(account,pwd,"account","sms_reg","")).request(new HttpCallbackProxy<HttpData<LoginUser>>(this){
                    @Override
                    public void onHttpSuccess(HttpData<LoginUser> result, boolean cache) {
                        super.onHttpSuccess(result, cache);
                        ToastUtils.show("注册成功");
                        LoginUser user = result.getData();
                        user.setAccount(account);
                        user.setPassword(pwd);
                        user.setUsername(account);
                        user.setPhoneAccount(false);
                        manager.saveUserAccount(user);
                        manager.writeAutoLogin(true);
                        manager.writeLastLoginType(1);
                        manager.saveCurrentLoginUserTickit(user.getAccess_token());
                        gotoMainActivity();
                    }
                });
            }
        }
    }




}
